import 'package:badges/badges.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:nyh_common_package/common/widgets/url_link_page.dart';
import 'package:nyh_motorcade_app/config/config.dart';
import 'package:nyh_motorcade_app/routes/routes.dart';
import 'package:nyh_motorcade_app/styles/colors.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:nyh_motorcade_app/utils/clipboard_utils.dart';
import 'package:nyh_motorcade_app/utils/toast_utils.dart';
import 'package:nyh_motorcade_app/widgets/multi_state/multi_state_widget.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'my_controller.dart';

class MyPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    Get.put(MyController());
    return AnnotatedRegion(
      value: SystemUiOverlayStyle.dark,
      child: Scaffold(
        backgroundColor: Colours.color_background,
        body: GetBuilder<MyController>(
          builder: (controller) {
            return MultiStateWidget(
              child: SmartRefresher(
                controller: controller.refreshController,
                onRefresh: controller.onRefresh,
                child: CustomScrollView(
                  slivers: [
                    buildHeaderAppBarWidget(controller),
                    buildFunctionMenuWidget(),
                  ],
                ),
              ),
              state: controller.multiState,
              onRetry: () {
                controller.onRetry();
              },
            );
          },
        ),
      ),
    );
  }

  Widget buildHeaderAppBarWidget(MyController controller) {
    return SliverAppBar(
      backgroundColor: Colours.color_background,
      elevation: 0,
      expandedHeight: 176.w - ScreenUtil().statusBarHeight,
      pinned: true,
      flexibleSpace: LayoutBuilder(
        builder: (context, constraints) {
          return FlexibleSpaceBar(
            background: buildHeaderWidget(controller),
            centerTitle: false,
            titlePadding: EdgeInsets.only(left: 16.w, bottom: 16.w),
            title: Visibility(
              visible: constraints.biggest.height <= ScreenUtil().statusBarHeight + 56,
              child: Text(
                controller.userName,
                style: TextStyle(
                  color: Colours.color_text_theme,
                  fontSize: 20.sp,
                  fontWeight: FontWeight.w500,
                ),
              ),
            ),
          );
        },
      ),
    );
  }

  Widget buildHeaderWidget(MyController controller) {
    return Container(
      width: double.infinity,
      height: 176.w,
      child: Stack(
        fit: StackFit.expand,
        children: [
          buildHeaderBackGroundWidget(),
          //buildMessageNotificationWidget(),
          buildUserInfoWidget(controller),
          buildCompanyInfoWidget(controller),
        ],
      ),
    );
  }

  Widget buildHeaderBackGroundWidget() {
    return Positioned(
      child: Image.asset(
        "images/bg_my_header.png",
        fit: BoxFit.fill,
      ),
    );
  }

  Widget buildMessageNotificationWidget() {
    return Positioned(
      top: ScreenUtil().statusBarHeight + 16.w,
      right: 16.w,
      child: GestureDetector(
        onTap: () {},
        child: Badge(
          position: BadgePosition.topEnd(top: -6.w, end: -6.w),
          padding: EdgeInsets.all(5.w),
          showBadge: false,
          animationType: BadgeAnimationType.scale,
          child: Image.asset("images/ic_message_notification.png"),
        ),
      ),
    );
  }

  Widget buildUserInfoWidget(MyController controller) {
    return Positioned(
      left: 16.w,
      right: 16.w,
      bottom: 52.w,
      child: Row(
        children: [
          ClipOval(
            child: buildAvatarWidget(controller),
          ),
          SizedBox(
            width: 16.w,
          ),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  controller.userName,
                  style: TextStyle(
                    color: Colours.color_text_theme,
                    fontSize: 18.sp,
                    fontWeight: FontWeight.bold,
                  ),
                  overflow: TextOverflow.ellipsis,
                ),
                SizedBox(
                  height: 5.w,
                ),
                Text(
                  controller.userPhone,
                  style: TextStyle(
                    color: Colours.color_9fa0ab,
                    fontSize: 14.sp,
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  Widget buildAvatarWidget(MyController controller) {
    Widget defaultAvatarWidget = Image.asset(
      "images/ic_avatar_default.png",
      fit: BoxFit.cover,
      height: 60.w,
      width: 60.w,
    );
    return controller.userIcon.isNotEmpty
        ? CachedNetworkImage(
            height: 60.w,
            width: 60.w,
            fit: BoxFit.cover,
            imageUrl: controller.userIcon,
            placeholder: (context, ur) {
              return defaultAvatarWidget;
            },
            errorWidget: (context, url, error) {
              return defaultAvatarWidget;
            },
          )
        : defaultAvatarWidget;
  }

  Widget buildCompanyInfoWidget(MyController controller) {
    return Positioned(
      left: 16.w,
      right: 16.w,
      bottom: 0,
      height: 36.w,
      child: Container(
        padding: EdgeInsets.only(left: 16.w, right: 8.w),
        decoration: BoxDecoration(
          color: Colours.color_white,
          borderRadius: BorderRadius.only(
            topLeft: Radius.circular(10.w),
            topRight: Radius.circular(10.w),
          ),
        ),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Expanded(
              flex: 2,
              child: Text(
                controller.companyName,
                style: TextStyle(
                  color: Colours.color_text_theme,
                  fontSize: 14.sp,
                ),
                maxLines: 1,
                overflow: TextOverflow.ellipsis,
              ),
            ),
            SizedBox(
              width: 5.w,
            ),
            Expanded(
              flex: 3,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.end,
                children: [
                  Text(
                    "资金账号 ",
                    style: TextStyle(
                      color: Colours.color_cfcfd5,
                      fontSize: 12.sp,
                      height: 1.3,
                    ),
                  ),
                  Text(
                    controller.companyCode,
                    style: TextStyle(
                      color: Colours.color_9fa0ab,
                      fontSize: 12.sp,
                    ),
                  ),
                  GestureDetector(
                    behavior: HitTestBehavior.opaque,
                    onTap: () {
                      ClipboardUtils.setData("${controller.companyName} ${controller.companyCode}");
                      ToastUtils.toast("已复制到粘贴板");
                    },
                    child: Padding(
                      padding: EdgeInsets.all(8.w),
                      child: Image.asset(
                        "images/ic_copy.png",
                        height: 12.w,
                        width: 12.w,
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget buildFunctionMenuWidget() {
    return SliverToBoxAdapter(
      child: Container(
        padding: EdgeInsets.only(left: 16.w, top: 12.w, right: 16.w, bottom: 16.w),
        child: Column(
          children: [
            buildFunctionMenuItemWidget(
              "我要合作",
              "images/ic_my_cooperate.png",
              BorderRadius.circular(10.w),
            ),
            SizedBox(
              height: 12.w,
            ),
            buildFunctionMenuItemWidget(
              "联系我们",
              "images/ic_my_contact_us.png",
              BorderRadius.only(
                topLeft: Radius.circular(10.w),
                topRight: Radius.circular(10.w),
              ),
            ),
            buildFunctionMenuItemWidget(
              "意见反馈",
              "images/ic_my_feedback.png",
              BorderRadius.only(
                bottomLeft: Radius.circular(10.w),
                bottomRight: Radius.circular(10.w),
              ),
            ),
            SizedBox(
              height: 12.w,
            ),
            buildFunctionMenuItemWidget(
              "设置",
              "images/ic_my_setting.png",
              BorderRadius.circular(10.w),
            ),
            SizedBox(
              height: 12.w,
            ),
          ],
        ),
      ),
    );
  }

  Widget buildFunctionMenuItemWidget(String name, String icon, BorderRadius? borderRadius) {
    return Container(
      decoration: BoxDecoration(
        color: Colours.color_white,
        borderRadius: borderRadius,
      ),
      child: ClipRRect(
        borderRadius: borderRadius,
        child: Material(
          child: InkWell(
            onTap: () {
              switch (name) {
                case "我要合作":
                  //Get.toNamed(Routes.cooperate);
                  UrlLinkPage.launchUrlPage(Get.context!, Config.getCooperateUrl());
                  break;
                case "联系我们":
                  Get.toNamed(Routes.contactUs);
                  break;
                case "意见反馈":
                  Get.toNamed(Routes.feedback);
                  break;
                case "设置":
                  Get.toNamed(Routes.setting);
                  break;
                default:
              }
            },
            child: Padding(
              padding: EdgeInsets.symmetric(vertical: 12.w, horizontal: 16.w),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Row(
                    children: [
                      Image.asset(
                        icon,
                        height: 24.w,
                        width: 24.w,
                      ),
                      SizedBox(
                        width: 16.w,
                      ),
                      Text(
                        name,
                        style: TextStyle(
                          color: Colours.color_text_theme,
                          fontSize: 14.sp,
                        ),
                      )
                    ],
                  ),
                  Image.asset("images/ic_arrow_right_gray.png"),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }
}
